package com.zpark.consumer.controller;

import com.zpark.consumer.entity.Books;
import com.zpark.consumer.utils.ResponseResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;

import java.util.List;

/**
 * @PackageName: com.zpark.consumer.controller
 * @ClassName: ConsumerController
 * @Description:
 * @author: RZS
 * @date: 2023/6/13  14:50
 */
@RestController
@RequestMapping("/consumer")
public class ConsumerController {

    @Autowired
    private RestTemplate restTemplate;

    @Value("${server-url.path}")
    private String serverPath;

    @PostMapping("/find/{id}")
    public ResponseResult<Books> findById(@PathVariable("id") Integer id){
        return new ResponseResult(2000, "成功",
                restTemplate.postForObject(serverPath + "books/find/" + id, null,
                        ResponseResult.class));
    }

    @GetMapping("/find_all")
    public ResponseResult<List<Books>>  findAll(){
        return new ResponseResult(2000, "成功",
                restTemplate.getForObject(serverPath + "books/find_all", ResponseResult.class));
    }

    @DeleteMapping("/del/{id}")
    public ResponseResult<Void>  removeById(@PathVariable("id") Integer id){
        // 实现服务调用
        restTemplate.delete(serverPath + "books/del/" + id);
        return new ResponseResult(2000, "成功", null);
    }

    // 返回当前服务的名字+端口号
    @GetMapping("/test")
    public ResponseResult<String> test(){

        return new ResponseResult(200, "成功",
                restTemplate.getForObject(serverPath + "books/test", String.class));
    }
}
